﻿using System;
using System.Web;
using System.Web.ModelBinding;

namespace Morus.Presentation
{
    public sealed class HttpFileCollectionValueProviderFactory : ValueProviderFactory
    {
        public override IValueProvider GetValueProvider(ModelBindingExecutionContext modelBindingExecutionContext)
        {
            Check.Argument.IsNotNull(modelBindingExecutionContext, "modelBindingExecutionContext");

            var fileRequest = IsFileRequest(modelBindingExecutionContext.HttpContext);

            if (fileRequest == false)
            {
                return null;
            }

            return new HttpFileCollectionValueProvider(modelBindingExecutionContext);
        }

        private static bool IsFileRequest(HttpContextBase httpContext)
        {
            if (!httpContext.Request.ContentType.StartsWith("multipart/form-data", StringComparison.OrdinalIgnoreCase))
            {
                // not has file info request
                return false;
            }
            return true;
        }
    }
}
